package com.es.search.service;

import java.util.List;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder;
import org.elasticsearch.search.suggest.term.TermSuggestionBuilder;
import org.springframework.stereotype.Service;

import com.es.client.elasticesearch.ESClient;
import com.es.common.utils.StringUtils;
import com.es.search.param.ProductSearchParam;

@Service("productSearchService")
public class ProductSearchService {
	
	private static final String INDEX = "indexdb";
	private static final String TYPE = "product";
	public static void main(String[] args) {
		ProductSearchService searchService = new ProductSearchService();
		/*ProductSearchParam param = new ProductSearchParam();
		//param.setStoreid(58);
		param.setKeyword("纸巾");
		param.setStart(0);
		param.setLimit(20);
		param.setStartPrice(3000);
		//param.setEndPrice(4000);
		int[] id = {44};
		param.setExcludeIds(id);*/
		//searchService.search(param);
		searchService.sse("");
	}
	public List<Integer> search (ProductSearchParam param){
		
		BoolQueryBuilder bool = QueryBuilders.boolQuery();
		if (param.getStoreid() > 0) {
			bool.must(QueryBuilders.termQuery("storeid", param.getStoreid()).boost(0.5f));
		}
		
		
		if (!"".equals(param.getKeyword())) {
			String[] fields = {"name","pingyinname"};
			QueryBuilder query = QueryBuilders.multiMatchQuery(param.getKeyword(), fields);
			//query = QueryBuilders.matchQuery("name", param.getKeyword());
			bool.must(query);
		}
		
		if (param.getStartPrice() > 0 || param.getEndPrice() > 0) {
			RangeQueryBuilder query = QueryBuilders.rangeQuery("priceValue");
			if (param.getStartPrice() > 0) {
				query.from(param.getStartPrice());
			}
			if (param.getEndPrice() > 0) {
				query.to(param.getEndPrice());
			}
			bool.must(query);
		}
		if (param.getExcludeIds() != null && param.getExcludeIds().length > 0) {
			TermsQueryBuilder query = QueryBuilders.termsQuery("productid", param.getExcludeIds());
			bool.mustNot(query);
		}
		SearchSourceBuilder source = new SearchSourceBuilder();
		source.query(bool);
		//source.fetchSource("productid", null);
		source.from(param.getStart());
		source.size(param.getLimit());
		if (StringUtils.isNotEmpty(param.getOrderBy())) {
			source.sort("priceValue", SortOrder.DESC);
		}
		//source.sort
		/*HighlightBuilder highlight = new HighlightBuilder()
		.field("name")
		.preTags("<em>")
		.preTags("</em>");
		source.highlighter(highlight);*/
		SearchHits hits = ESClient.getEsClient().querySearch(INDEX, TYPE,  source);
		System.out.println("共匹配到:"+hits.getTotalHits()+"条记录!");
		for (SearchHit hit : hits) {
			System.out.println(hit.getSourceAsString());

		}
		return null;
	}
	
	public void sse(String keyword){
		SuggestBuilder suggestBuilder = new SuggestBuilder();
		//https://elasticsearch.cn/article/142
		//https://github.com/laoyang360/deep_elasticsearch/blob/master/02_suggester.md
		//https://discuss.elastic.co/t/what-are-the-best-approach-for-chinese-japanese-language-indexing-and-searching/79
		CompletionSuggestionBuilder phraseSuggestionBuilder = new CompletionSuggestionBuilder("name");
		phraseSuggestionBuilder.field();
		phraseSuggestionBuilder.text("苹果");
		phraseSuggestionBuilder.size(5);
		suggestBuilder.addSuggestion(TYPE, phraseSuggestionBuilder);
		SearchHits hits = ESClient.getEsClient().suggestion(INDEX, TYPE, suggestBuilder);
		for (SearchHit hit : hits) {
			System.out.println(hit.getSourceAsString());

		}
		

		
	}
	
	

}
